package com.magicmovie.web.controller;

import com.magicmovie.web.service.CustomerService;
import com.magicmovie.web.service.MovieService;
import com.magicmovie.web.service.OrderService;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/")
    public String index(Model model) {
        Integer customerCount = customerService.queryCount();
        model.addAttribute("customerCount", customerCount);
        Integer movieCount = movieService.queryCount(new HashMap<>());
        model.addAttribute("movieCount", movieCount);
        //1： 统计总付费用户 2:总订单数 3：订单总价 4：总域名数
        Integer payedCustomer=orderService.statisticsByType(1);
        Integer orderCount=orderService.statisticsByType(2);
        Integer orderAmount=orderService.statisticsByType(3);
        Integer siteCount=orderService.statisticsByType(4);
        model.addAttribute("payedCustomerCount", payedCustomer);
        model.addAttribute("orderCount", orderCount);
        model.addAttribute("orderAmount", orderAmount);
        model.addAttribute("siteCount", siteCount);

        List<Map<String,Object>> bestSellingList =orderService.bestSellingList();
        List<Map<String,Object>> mostPaidUser =orderService.mostPaidUser();
        model.addAttribute("bestSellingList", bestSellingList);
        model.addAttribute("mostPaidUser", mostPaidUser);

        return "index";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, Map<String, Object> map) throws Exception {
        System.out.println("HomeController.login()");
        // 登录失败从request中获取shiro处理的异常信息。
        // shiroLoginFailure:就是shiro异常类的全类名.
        String exception = (String) request.getAttribute("shiroLoginFailure");
        System.out.println("exception=" + exception);
        String msg = "";
        if (exception != null) {
            if (UnknownAccountException.class.getName().equals(exception)) {
            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
            } else {
                System.out.println("else -- >" + exception);
            }
            msg = "账号或密码错误！";
        }
        map.put("msg", msg);
        // 此方法不处理登录成功,由shiro进行处理
        return "login";
    }
//
//    @RequestMapping("/403")
//    public String unauthorizedRole(){
//        System.out.println("------没有权限-------");
//        return "403";
//    }

}